package com.caiyi.financial.nirvana.sms.controller;

import com.alibaba.fastjson.JSON;
import com.caiyi.financial.nirvana.sms.service.SmsMsgService;
import com.youyu.common.api.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

/**
 * Created by Mario on 2018/3/12 0012.
 * 回调测试专用
 */
@Api(description = "回调测试")
@CrossOrigin
@RestController
@RequestMapping("sms/test/callback")
public class CallBackTestController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SmsMsgService smsMsgService;


    /**
     * 测试
     *
     * @return 测试结果
     */
    @ApiOperation(value = "测试接口信息", notes = "测试信息")
    @RequestMapping(value = "/post", method = RequestMethod.POST)
    public void test(@RequestBody Map params, HttpServletRequest request, HttpServletResponse response) {
        logger.info("Request Headers ========================================================");
        Enumeration names = request.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            logger.info(name + ":" + request.getHeader(name));
        }
        logger.info("===================================================================");
        logger.info("Request Body：" + JSON.toJSONString(params));
        try {
            response.getWriter().write("SUCCESS");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @ApiOperation(value = "修改统计结果", notes = "修改统计结果")
    @RequestMapping(value = "/updateMsgCount", method = RequestMethod.POST)
    public Result updateMsgCount(@RequestBody Map params, HttpServletResponse response) {
        logger.info("updateMsgCount : {}", params);
        int daysDiff = Integer.parseInt(params.get("daysDiff") + "");
        for (int i = 4; i <= daysDiff + 4; i++) {
            smsMsgService.testUpdateMsgCount(i, i - 1);
        }
        return Result.ok();
    }

}
